//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DiscretizedAsset.h"
using namespace Cephei::QL;
#include <gen/QL/Lattice.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CDiscretizedAsset::CDiscretizedAsset (boost::shared_ptr<QuantLib::DiscretizedAsset>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscretizedAsset = NULL;
#endif
	_ppDiscretizedAsset = &childNative;
    
}
Cephei::QL::CDiscretizedAsset::CDiscretizedAsset (QuantLib::DiscretizedAsset& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDiscretizedAsset = NULL;
#endif
	_ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (&childNative);
    
    _DiscretizedAssetOwner = owner;
    
}

Cephei::QL::CDiscretizedAsset::CDiscretizedAsset (CDiscretizedAsset^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscretizedAsset = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (copy->GetShared());
        
    }
}
Cephei::QL::CDiscretizedAsset::CDiscretizedAsset (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscretizedAsset = NULL;
#endif
	if (!t->IsSubclassOf(CDiscretizedAsset::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CDiscretizedAsset::CDiscretizedAsset (QuantLib::Handle<QuantLib::DiscretizedAsset>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDiscretizedAsset = &childNative;
	_ppDiscretizedAsset = &static_cast<boost::shared_ptr<QuantLib::DiscretizedAsset>>(childNative.currentLink());
    
    _DiscretizedAssetOwner = owner;
}
Cephei::QL::CDiscretizedAsset::CDiscretizedAsset (QuantLib::Handle<QuantLib::DiscretizedAsset> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDiscretizedAsset = &childNative;
	_ppDiscretizedAsset = &static_cast<boost::shared_ptr<QuantLib::DiscretizedAsset>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CDiscretizedAsset::CDiscretizedAsset (QuantLib::DiscretizedAsset childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDiscretizedAsset = NULL;
#endif
	_ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (new QuantLib::DiscretizedAsset (childNative));
    
}
#endif

Cephei::QL::CDiscretizedAsset::~CDiscretizedAsset ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDiscretizedAsset != NULL)
    {
	    delete _ppDiscretizedAsset;
        _ppDiscretizedAsset = NULL;
    }
}
Cephei::QL::CDiscretizedAsset::!CDiscretizedAsset ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDiscretizedAsset != NULL)
    {
	    delete _ppDiscretizedAsset;
    }
}
QuantLib::DiscretizedAsset& Cephei::QL::CDiscretizedAsset::GetReference ()
{
    if (_ppDiscretizedAsset == NULL) throw REFNEW NativeNullException ();
	return **_ppDiscretizedAsset;
}
boost::shared_ptr<QuantLib::DiscretizedAsset>& Cephei::QL::CDiscretizedAsset::GetShared ()
{
    if (_ppDiscretizedAsset == NULL) throw REFNEW NativeNullException ();
	return *_ppDiscretizedAsset;
}
QuantLib::DiscretizedAsset* Cephei::QL::CDiscretizedAsset::GetPointer ()
{
    if (_ppDiscretizedAsset == NULL) throw REFNEW NativeNullException ();
	return &**_ppDiscretizedAsset;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscretizedAsset>& Cephei::QL::CDiscretizedAsset::GetHandle ()
{
	if (_phDiscretizedAsset == NULL)
	{
		_phDiscretizedAsset = new Handle<QuantLib::DiscretizedAsset> (*_ppDiscretizedAsset);
	}
	return *_phDiscretizedAsset;
}
#endif
bool Cephei::QL::CDiscretizedAsset::HasNative () 
{
	return (_ppDiscretizedAsset != NULL);
}

Cephei::QL::IDiscretizedAsset^ Cephei::QL::CDiscretizedAsset::AdjustValues::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppDiscretizedAsset)->adjustValues ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedAsset^ Cephei::QL::CDiscretizedAsset::Initialize (Cephei::QL::ILattice^ prm1, Double t)
{
    CLattice^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CLattice^> (prm1);
        _Cprm1->Lock();
        boost::shared_ptr<QuantLib::Lattice>& _prm1 = static_cast<boost::shared_ptr<QuantLib::Lattice>&> (_Cprm1->GetShared ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	(*_ppDiscretizedAsset)->initialize ( _prm1,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::CDiscretizedAsset::MandatoryTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppDiscretizedAsset)->mandatoryTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ILattice^ Cephei::QL::CDiscretizedAsset::Method::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Lattice>& _rv = (boost::shared_ptr<QuantLib::Lattice>&)(*_ppDiscretizedAsset)->method ( );   
        Cephei::QL::CLattice^ _nrv = REFNEW Cephei::QL::CLattice (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedAsset^ Cephei::QL::CDiscretizedAsset::PartialRollback (Double to)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _to = (QuantLib::Time)ValueHelper::Convert (to); //a
    	(*_ppDiscretizedAsset)->partialRollback ( _to );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedAsset^ Cephei::QL::CDiscretizedAsset::PostAdjustValues::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppDiscretizedAsset)->postAdjustValues ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedAsset^ Cephei::QL::CDiscretizedAsset::PreAdjustValues::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppDiscretizedAsset)->preAdjustValues ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CDiscretizedAsset::PresentValue::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDiscretizedAsset)->presentValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedAsset^ Cephei::QL::CDiscretizedAsset::Reset (UInt64 size)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size); //a
    	(*_ppDiscretizedAsset)->reset ( _size );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedAsset^ Cephei::QL::CDiscretizedAsset::Rollback (Double to)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _to = (QuantLib::Time)ValueHelper::Convert (to); //a
    	(*_ppDiscretizedAsset)->rollback ( _to );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CDiscretizedAsset::Time::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppDiscretizedAsset)->time ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::CDiscretizedAsset::Values::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppDiscretizedAsset)->values ( );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

